<template>
	<view>
		<u--input class="search" placeholder="Please enter username" border="false" v-model="queryParams.search"  @keyup.enter="change" @blur="change"></u--input>

		<view class="" v-for="(item, index) in userList" :key="index">
			<ListItem :title="item.real_name" :details="item.major" link @click.native="jump(item)">
				<template #photo>
					<!-- <image src="../../static/img/logo.png" mode="" style="width: 84rpx;height: 84rpx;"></image> -->
					<u-avatar :text="item.real_name.charAt(0).toUpperCase()" fontSize="18" randomBgColor></u-avatar>
				</template>
			</ListItem>
		</view>
		<empty v-show="userList.length==0"></empty>
	</view>
</template>

<script>
	import ListItem from '@/components/basicListItem/ShadowyListItem/ShadowyListItem.vue'
	import common from '@/api/common.js'
	export default {
		components: {
			ListItem
		},

		data() {
			return {
				queryParams: {
					page: 1,
					search:'',
				},
				userList: [],
				lastPage:''
			}
		},
		onLoad() {
			this.getList()
		},
		// 底部上拉刷新
		onReachBottom(){
			if(this.lastPage > 1 && this.queryParams.page <= this.lastPage){    
				//判断是否还有数据需要加载
					uni.showLoading({
						title: 'Loading'
					});
					setTimeout(function () {
						uni.hideLoading();
					}, 2000);
					this.queryParams.page ++
					this.getList()
				}else{
					uni.showToast({
						title: 'nomoreText',
						icon:'none'
					});
				}
		},
		methods: {
			jump(e) {
				//加密，JSON.stringify解析到空值报错
				const user = encodeURIComponent(JSON.stringify(e))
				uni.navigateTo({
					url: './components/usersDetails/usersDetails?user=' + user
				});
			},
			getList() {
			  common.getUserList(this.queryParams).then((res) => {
			    if (this.queryParams.page === 1) {
			      this.userList = res.data.list.data;
			    } else {
			      this.userList = this.userList.concat(res.data.list.data);
			    }
			    this.lastPage = res.data.list.last_page;
				this.$store.dispatch('userList',res.data.list.data)
			  });
			},
			change(e) {
			  uni.showLoading({
			  	title: 'Loading'
			  });
			  this.queryParams.page = 1; // 重置页码为1
			  this.userList = []; // 清空数组
			  this.debounce(() => {
			  	this.getList()
			  }, 500);
			}
		}
	}
</script>

<style>
.search {
		width: 700rpx;
		height: 66rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 6rpx 12rpx 2rpx rgba(174, 174, 174, 0.16);
		border-radius: 100rpx;
		margin: 24rpx auto;
	}
</style>